﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Azad;
using Azad.BCL;
using BCL.Entity;
using NazSoft.Framework;

public partial class Idaria : System.Web.UI.Page
{
    protected string _logoId = string.Empty;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.QueryString["mp"] == "b")
        {
            this.MasterPageFile = "~/BlankMasterPage.master";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var collection = Request.QueryString;
        String mn = collection["mn"] ?? string.Empty; // menu name
        string mp = collection["mp"];
        bool preview = Convert.ToBoolean(collection["preview"]);
        bool isHome = false;


        if (Page.IsPostBack == false)
        {
            SilverlightService silverlightService = new SilverlightService();
            var today = DateTimeUtilities.GetDateFromQueryString(collection).ToString();

            var logoInfo = silverlightService.GetByCurrentId(today, preview);
            if (logoInfo != null)
            {
                _logoId = Convert.ToString(logoInfo.Id);
                imgIdaria.Src = logoInfo.HeaderImage;
                // over-ride date here to be used later in databound method
                //today = logoInfo.CurrentDate;

                // first get the details of specific menu
                var details = logoInfo.Detail.Where(x => x.MenuName == mn).FirstOrDefault();
                CurrentUrl = logoInfo.Url;
                if (String.IsNullOrEmpty(mn))
                {
                    isHome = true;
                }

                if (isHome)
                {
                    //CoverPagePlaceHolder1.Visible = true;
                    //ConverPageImage.ImageUrl = logoInfo.LogoImage;
                    //CoverPageLink.NavigateUrl = logoInfo.Url + "current-magazine";
                }

                List<LogoDetailDetail> itemsList = null;

                var pageSize = 0;

                if (details != null)
                {
                    itemsList = details.DetailToDetail;
                    pageSize = details.NoOfFilePerPage;
                }
                else
                {
                    // only search for isHome items if no menu name is defined in querystring
                    if (String.IsNullOrEmpty(mn))
                    {
                        itemsList = new List<LogoDetailDetail>();

                        // find all items of is home
                        foreach (var item in logoInfo.Detail)
                        {
                            var d = item.DetailToDetail.Where(x => x.IsHome == true);
                            itemsList.AddRange(d);
                        }
                    }
                }

                var currentPage = (collection["page"].ToInt32Safe());
                //BindData(itemsList, pageSize, currentPage, logoInfo.Url, mn);


            }
            else
            {
                //NewsNotFoundMessage.Visible = true;
                //NewsThumbnails.Visible = false;
                //NextPageHyperlink.Visible = false;
                //PrevPageHyperLink.Visible = false;
            }
        }

    }

    public string CurrentUrl { get { return ViewState["test"] as string; } set { ViewState["test"] = value; } }
}